package com.aidong.dao.base;

import java.io.Serializable;
import java.util.List;

public class Pagination<T> implements Serializable {
    private int totalCount;
    private int pageCount;
    private int pageNo;
    private int pageSize;
    private List<T> resultList;

    public Pagination() {
    }

    public Pagination(int pageNo, int pageSize) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageCount() {
        return this.pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageNo() {
        return this.pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public List<T> getResultList() {
        return this.resultList;
    }

    public void setResultList(List<T> resultList) {
        this.resultList = resultList;
    }

    public void setPage(Integer pageNo, Integer totalCount) {
        if (pageNo != null && pageNo.intValue() > 0) {
            this.pageNo = pageNo.intValue();
        }

        if (totalCount != null && totalCount.intValue() > 0) {
            this.totalCount = totalCount.intValue();
        }

    }

    public String getCountHtml() {
        return "<div class=\"lsinfo\">查询结果： 共" + this.totalCount + "条记录 / 显示" + this.pageCount + "页 </div>";
    }

    public String getPageHtml() {
        StringBuilder sb = new StringBuilder();
        sb.append("<div class=\"page\"><span>共" + this.totalCount + "记录</span>");
        if (this.pageNo > 1) {
            sb.append("<a class=\"first\" href=\"javascript:topage(1);\">首页</a>");
            sb.append("<a class=\"prev\" href=\"javascript:topage(" + (this.pageNo - 1) + ");\">上页</a>");
        } else {
            sb.append("<span class=\"first\">首页</span>");
            sb.append("<span class=\"prev\">上页</span>");
        }

        if (this.pageNo < this.pageCount) {
            sb.append("<a class=\"next\" href=\"javascript:topage(" + (this.pageNo + 1) + ");\">下页</a>");
            sb.append("<a class=\"last\" href=\"javascript:topage(" + this.pageCount + ");\">末页</a>");
        } else {
            sb.append("<span class=\"next\">下页</span>");
            sb.append("<span class=\"last\">末页</span>");
        }

        sb.append("<span>第<input type=\"text\" value=\"" + this.pageNo + "\" size=\"3\" maxlength=\"4\" onkeydown=\'pagekeydown(event,this.value);\'>页</span>");
        sb.append("<span>共" + this.pageCount + "页</span>");
        sb.append("</div>");
        return sb.toString();
    }

    public String getPageHtml2() {
        StringBuilder sb = new StringBuilder();
        if (this.pageCount > 1) {
            sb.append("<div class=\"pages\">");
            int p1 = this.pageNo - 1;
            if (p1 < 1) {
                p1 = 1;
            }

            int p2 = p1 + 2;
            if (p2 > this.pageCount) {
                p2 = this.pageCount;
                p1 = p2 - 2;
                if (p1 < 1) {
                    p1 = 1;
                }
            }

            if (this.pageNo > 1) {
                sb.append("<a  href=\"javascript:gopg(" + (this.pageNo - 1) + ");\">上页</a>");
            }

            if (p1 > 1) {
                sb.append("<a href=\"javascript:gopg(1);\">1</a>");
            }

            if (p1 > 2) {
                sb.append("<span>...</span>");
            }

            for (int i = p1; i <= p2; ++i) {
                if (i == this.pageNo) {
                    sb.append("<span>").append(i).append("</span>");
                } else {
                    sb.append("<a href=\"javascript:gopg(").append(i).append(");\">").append(i).append("</a>");
                }
            }

            if (p2 < this.pageCount - 1) {
                sb.append("<span>...</span>");
            }

            if (p2 < this.pageCount) {
                sb.append("<a href=\"javascript:gopg(").append(this.pageCount).append(");\">").append(this.pageCount).append("</a>");
            }

            if (this.pageNo < this.pageCount) {
                sb.append("<a href=\"javascript:gopg(" + (this.pageNo + 1) + ");\">下页</a>");
            }

            sb.append("</div>");
        }

        return sb.toString();
    }
}
